define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["100%",'100%']);
            $(".btn-edit").data('area',["100%",'100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/diff/index' + location.search,
                    add_url: 'purchase/diff/add',
                    edit_url: 'purchase/diff/edit',
                    // del_url: 'purchase/diff/del',
                    multi_url: 'purchase/diff/multi',
                    table: 'purchase_diff',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 4) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'diff_id'},
                        {field: 'actual_cost', title: __('Actual_cost'), operate:'BETWEEN',operate:false},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},
                        {field: 'supplier.nickname', title: __('Supplier.nickname')},
                        {field: 'storehouse.name', title: __('storehouse.name')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=5")},
                        {field: 'explain', title: __('Explain')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var ids =$(this).parents('tr').find('.diff_id').html()
                                        var url = Config.this_url+'/purchase/diff/detail?ids='+ids
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                // console.log(value)
                                // console.log(row)
                                var that = $.extend({}, this);
                                if(row.ystatus !=1){
                                    $(table).data("operate-edit", null);
                                }
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                if($(this).parents('tr').find('.btn-editone').length >0){
                    return  false;
                }
                var diff_id = $(this).parents('tr').find('.diff_id').html()
                var url = Config.this_url+'/purchase/diff/detail?ids='+diff_id
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "入库详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {
                var house_id = $('#c-storehouse_id').val()
                var supplier_id = $('#c-supplier_id').val()
                // console.log(house_id)
                // console.log(supplier_id)
                if(house_id <1 ){
                    layer.msg('仓库或供应商不能为空')
                    return false;
                }

                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: Config.this_url+'/purchase/diff/seach?storehouse_id='+house_id+'&supplier_id='+supplier_id,
                })
            })
            Controller.api.bindevent();
        },
        seach:function(){
            // console.log(location.search);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/specs' ,
                    table: '',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'title', title: __('title'),  operate: false},
                        {field: 'place_origin', title: __('place_origin'), formatter: Table.api.formatter.search},
                        {field: 'number', title: __('number')},
                        {field: 'unit', title: __('unit'), align: 'left'},
                        {field: 'specs', title: __('specs')},
                        {field: 'stock', title: __('stock')},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){

                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);

            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/purchase/diff/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })

            // $('.subpurchase').click(function () {
            //     layer.confirm('审核通过？', {
            //         btn: ['确定','取消'] //按钮
            //     }, function(){
            //         var url = Config.this_url+'/purchase/enter/examine?ids='+$('#enter_id').val()
            //         $.post(url,{status:2},function (res) {
            //             layer.msg(res.msg)
            //             if(res.code == 1){
            //                 parent.$(".btn-refresh").trigger("click");
            //                 var index = parent.Layer.getFrameIndex(window.name);
            //                 parent.Layer.close(index);
            //             }
            //         })
            //     }, function(){
            //
            //     });
            // })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});